// hyb_yl/mysubpages/pages/openservice/openservice.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    twcheck: false,
    timeLeft: '',
    zxcheck: false,
    timezx: '',
    service: [{
        icon: '../../../images/service1.png',
        title: '图文问诊',
        price: 2,
        tip: '通过文字、图片向医生提问，含2次追op问的机会适合常见的病及健康类问题咨询。',
        twcheck: false,
        isopen: true,
        time: ''
      },
      {
        icon: '../../../images/service2.png',
        title: '在线开方',
        price: 2,
        tip: '通过在线的方式向医生提供相关诊断，医生根据诊断开出相关的处方。',
        twcheck: true,
        isopen: true,
        time: '28天5小时'
      },
      {
        icon: '../../../images/service3.png',
        title: '报告解读',
        price: 2,
        tip: '在线向医生发送体检报告，医生查看报告后在线解读，可向医生提出相关问题。',
        twcheck: false,
        isopen: false,
        time: ''
      },
      {
        icon: '../../../images/service4.png',
        title: '视频问诊',
        price: 2,
        tip: '可以和医生通过视频的方式进行面对面的询问不用出门即可进行诊断。',
        twcheck: false,
        isopen: false,
        time: ''
      },
      {
        icon: '../../../images/service5.png',
        title: '电话问诊',
        price: 2,
        tip: '通过打电话的方式向医生描述自己的病况，医生根据病况实时解答，但只适合初步诊断。',
        twcheck: false,
        isopen: false,
        time: ''
      },
      {
        icon: '../../../images/service6.png',
        title: '实时会话',
        price: 2,
        tip: '与医生进行实时会话，以视频问诊和电话问诊类似，可以随叫随到，随时在线。',
        twcheck: false,
        isopen: false,
        time: ''
      },
      {
        icon: '../../../images/service7.png',
        title: '手术安排',
        price: 2,
        tip: '在线预约手术，进行手术的相关安排，省时省力，方便快捷。',
        twcheck: false,
        isopen: false,
        time: ''
      },
      {
        icon: '../../../images/service8.png',
        title: '其他服务',
        price: 2,
        tip: '除了以上服务以外的其他相关服务。',
        twcheck: false,
        isopen: false,
        time: ''
      }
    ],
    tabList: ['已开通', '未开通', '全部'],
    tabIndex: 0
  },
  tabChose: function (e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      tabIndex: index,
      type: index
    })
    this.getServelist(this.data.zid, index)
  },
  opening: function (e) {
    console.log(e);
    const state = e.currentTarget.dataset.state;
    var ids = e.currentTarget.dataset.ids
    var title = e.currentTarget.dataset.title
    wx.navigateTo({
      url: '/hyb_yl/mysubpages/pages/serviceset/serviceset?state=' + state + '&ids=' + ids + '&title=' + title,
    })
    return false
  },
  open: function () {
    console.log("开通");
    return false
  },
  twchange: function (e) {
    // console.log(e.currentTarget.dataset.index);
    var that = this
    const ischeck = e.detail.value;
    console.log(ischeck);
    var index = e.currentTarget.dataset.index;
    // this.data.service[index].twcheck = ischeck
    // console.log(this.data.service[index].twcheck);
    that.setData({
      ischeck: ischeck
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var zid = options.zid
    that.setData({
      zid: zid
    })

    var bgc = wx.getStorageSync('color')
    this.setData({
      bg_color: bgc
    })
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    this.getServelist(zid, '')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  getServelist(zid, type) {
    var that = this
    app.util.request({
      url: 'entry/wxapp/zhuanjia.servertime',
      data: {
        zid: zid,
        type: type
      },
      success: function (res) {
        console.log(res)
        that.setData({
          server: res.data
        })
      }
    })
  },
  twchanges(e) {
    var that = this
    var ids = e.currentTarget.dataset.ids
    var ischeck = e.currentTarget.dataset.stateback
    var money = e.currentTarget.dataset.money;
    var gq = e.currentTarget.dataset.gq;
    var time_leng = e.currentTarget.dataset.time_leng;
    var key_words = e.currentTarget.dataset.key_words;
    var titlme = e.currentTarget.dataset.titlme;
    var zid = that.data.zid;
    var bid = e.currentTarget.dataset.bid;
    if (ischeck == 0) {
      //开启
      wx.showModal({
        title: '提示',
        content: "确认开启该服务吗？",
        success: function (con) {
          if (con.confirm) {
            if (!gq || money == '0' || money == '0.00' || money == '') {
              app.util.request({
                'url': 'entry/wxapp/zhuanjia.update_switch',
                data: {
                  ids: ids,
                  stateback: 1,
                  time_leng: time_leng,
                  key_words: key_words,
                  titlme: titlme,
                  zid: zid,
                  bid: bid
                },
                success: function (res) {
                  console.log(res)
                  that.getServelist(that.data.zid, that.data.type)
                }
              })

            } else {
              app.util.request({
                'url': 'entry/wxapp/zhuanjia.server_pay',
                header: {
                  "Content-Type": "application/xml"
                },
                method: "GET",
                data: {
                  openid: wx.getStorageSync('openid'),
                  money: money,
                  ids: ids,
                  typs: 'team',

                },
                success: function (res) {
                  wx.requestPayment({
                    'timeStamp': res.data.timeStamp,
                    'nonceStr': res.data.nonceStr, //字符串随机数
                    'package': res.data.package,
                    'signType': res.data.signType,
                    'paySign': res.data.paySign,
                    success: function (res) {
                      app.util.request({
                        'url': 'entry/wxapp/zhuanjia.update_switch',
                        data: {
                          ids: ids,
                          stateback: 1,
                          ids: ids,
                          stateback: 1,
                          time_leng: time_leng,
                          key_words: key_words,
                          titlme: titlme,
                          zid: zid,
                          bid: bid
                        },
                        success: function (res) {
                          console.log(res)
                          that.getServelist(that.data.zid, that.data.type)
                        }
                      })
                    },
                    fail: function (res) {
                      console.log(res)
                    }
                  })
                }
              })
            }
          }
        }
      })

    } else {
      wx.showModal({
        title: '提示',
        content: "确认关闭该服务吗？",
        success: function (con) {
          if (con.confirm) {
            app.util.request({
              'url': 'entry/wxapp/zhuanjia.update_switch',
              data: {
                ids: ids,
                stateback: 0
              },
              success: function (res) {
                console.log(res)
                that.getServelist(that.data.zid, that.data.type)
              }
            })
          }
        }
      })
    }


    console.log(ids)
  }
})